<?php
/**
 * V6
 */
class data_tee_article extends data {
	public function __construct() {
		parent::__construct();
		
	}
	/**
	 * 发布tee给圈子成员，已经在tee中接受过的不发送。Unique(user_id, article_id)
	 *
	 * @param int $circle_article_id
	 * @return int
	 */
	public function add_by_member_circle ($circle_article_id) {
		$circle_article_id = intval($circle_article_id);
		
		if ($circle_article_id) {
			return $this->db->execute('
				insert ignore into
					tee
				(user_id, source_type, circle_id, circle_article_pkid, article_id, author_id, url_id, dt)
					select
						circle_member.user_id,
						"member_circle",
						circle_article.circle_id,
						circle_article.id,
						circle_article.article_id,
						circle_article.author_id,
						circle_article.url_id,
						circle_article.dt
					from
						circle_article, circle_member
					where
						circle_article.id			= "'.$circle_article_id.'"		and
						circle_member.circle_id 	= circle_article.circle_id
			');
		} else return false;
	}
	/**
	 * 发布tee给圈子follower，已经在tee中接受过的不发送。Unique(user_id, article_id)
	 *
	 * @param int $circle_article_id
	 * @return int
	 */
	public function add_by_follow_circle ($circle_article_id) {
		$circle_article_id = intval($circle_article_id);
		
		if ($circle_article_id) {
			return $this->db->execute('
				insert ignore into
					tee
				(user_id, source_type, circle_id, circle_article_pkid, article_id, author_id, url_id, dt)
					select
						follow_circle.user_id,
						"follow_circle",
						follow_circle.circle_id,
						circle_article.id,
						circle_article.article_id,
						circle_article.author_id,
						circle_article.url_id,
						circle_article.dt
					from
						circle_article, follow_circle
					where
						circle_article.id			= "'.$circle_article_id.'"		and
						follow_circle.circle_id 	= circle_article.circle_id
			');
		} else return false;
	}
	
	public function event_del_by_save_article ($article_id) {
		$article_id = intval($article_id);
		
		if ($article_id) {
			return $this->db->execute('
				delete from
					tee
				where
					article_id	= "'.$article_id.'"
			');
		} else return false;
	}
	
	/**
	 * 列出tee
	 * 
	 * @param int $uid
	 * @param string $source_type		// all / member_circle /  follow_circle
	 * @param int $source_id			// id=0时为全部
	 * @param int $page					// 最小为1
	 * @param int $limit				// 最小为1
	 * @return array
	 */
	public function ls ($uid, $source_type='all', $circle_id=0, $page=1, $limit=20) {
		$uid			= intval($uid);
		$source_type	= ($source_type==='member_circle' or $source_type==='follow_circle') ? $source_type : 'all';
		$circle_id		= intval($circle_id);
		$page			= ($page>1) ? $page : 1;
		$limit			= ($limit>1) ? $limit : 1;
		
		if ($uid) {
			// 根据source_type和source_id构造SQL
			if ($circle_id) {
				$sql = ' tee.circle_id = "'.$circle_id.'" ';
			} elseif ($source_type === 'member_circle' or $source_type === 'follow_circle') {
				$sql = ' tee.source_type = "'.$source_type.'" ';
			} else {
				$sql = ' 1 ';
			}
			$execute = $this->db->execute('
				select
					tee.id					AS id,
					tee.user_id				AS user_id,
					tee.source_type			AS source_type,
					tee.dt					AS dt,
					save_article.id			AS article_id,
					save_article.title		AS article_title,
					save_article.user_id	AS author_id,
					article_url.id			AS url_id,
					article_url.url			AS url,
					circle.title			AS circle_title
				from
					tee, save_article, circle, article_url
				where
					tee.user_id		= "'.$uid.'"			and
					'.$sql.'								and
					save_article.id	= tee.article_id		and
					article_url.id	= save_article.url_id	and
					circle.id		= tee.circle_id
				order by
					tee.id DESC
				limit
					'.(($page-1)*$limit).', '.$limit.'
			');
			if (is_array($execute)) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['friend_nickname'] = $data_user->friend_to_nickname($uid, $v['author_id']);
				}
				return $execute;
			} else return false;
		} else return false;
	}
	
	public function one ($uid, $id) {
		$uid	= intval($uid);
		$id 	= intval($id);
		if ($uid and $id) {
			$execute = $this->db->execute('
				select
					tee.id						AS id,
					save_article.id				AS article_id,
					save_article.id				AS content_id,
					save_article.user_id		AS user_id,
					save_article.title			AS title,
					save_article.dt				AS dt,
					article_url.id				AS url_id,
					article_url.url				AS url
				from
					tee, save_article, article_url
				where
					tee.id			= "'.$id.'"				and
					tee.user_id		= "'.$uid.'"			and
					save_article.id	= tee.article_id		and
					article_url.id	= save_article.url_id
			');
			return $execute[0];
		} else return false;
	}
	/**
	 * 当加入圈子的时候，需要把圈子中的内容复制到tee中
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $circle_id
	 * @return unknown
	 */
	public function event_copy_tee_from_member_circle ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			return $this->db->execute('
				insert ignore into
					tee
				(user_id, source_type, circle_id, circle_article_pkid, article_id, author_id, url_id, dt)
					select
						"'.$uid.'",
						"member_circle",
						circle_article.circle_id,
						circle_article.id,
						circle_article.article_id,
						circle_article.author_id,
						circle_article.url_id,
						"'.date('Y-m-d H:i:s').'"
					from
						circle_member, circle_article
					where
						circle_member.user_id		= "'.$uid.'"					and
						circle_member.circle_id		= "'.$circle_id.'"				and
						circle_article.circle_id	= circle_member.circle_id
					order by
						circle_article.id ASC
			');
		} else return false;
	}
}
?>